<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="checkbox" id="checkAll">全选
    <button id="btn">反选</button>
    <ul>
        <li><input type="checkbox" class="ck" id="1">张三</li>
        <li><input type="checkbox" class="ck" id="2">李四</li>
        <li><input type="checkbox" class="ck" id="3">皮麻子</li>
    </ul>
    <script>
        // 全选
        const checkAll = document.querySelector('#checkAll')
        const cks = document.querySelectorAll('.ck')
        checkAll.addEventListener('click', function () {
            for (let i = 0; i < cks.length; i++) {
                cks[i].checked = checkAll.checked
            }
        })
        // 小复选框控制大复选框
        for (let i = 0; i < cks.length; i++) {
            cks[i].addEventListener('click', function () {
                const ck = document.querySelectorAll('.ck:checked')
                if (ck.length === cks.length) {
                    checkAll.checked = true
                } else {
                    checkAll.checked = false
                }
            })
        }
        // 反选
        const btn = document.querySelector('#btn')
        btn.addEventListener('click', function () {
            for (let i = 0; i < cks.length; i++) {
                cks[i].click()
            }
        })
    </script>
</body>

</html>